<?php

namespace app\api\controller;


use app\api\biz\UserBiz;
use app\common\Cache;
use app\common\controller\ApiController;
use app\common\enum\Cache as CacheEnum;
use app\common\model\biz\SysUser;
use app\common\Result;
use joncc\attribute\attr\AuthNeedless;
use joncc\attribute\attr\Comment;
use joncc\attribute\attr\Params;
use joncc\attribute\attr\PermissionDefine;
use joncc\attribute\attr\PermissionNeedless;
use think\response\Json;


#[Comment('用户')]
#[AuthNeedless]
#[PermissionNeedless('setting')]
#[PermissionDefine('list', '列表')]
#[PermissionDefine('add', '新增')]
#[PermissionDefine('edit', '编辑')]
#[PermissionDefine('del', '删除')]
#[PermissionDefine('setting', '个人设置')]
#[PermissionDefine('setUserPassword', '设置用户密码')]
class User extends ApiController
{

    #[Params('name', Params::STRING, false, '名称')]
    #[Params('field', Params::STRING, false, "查询字段','分隔")]
    #[Params('sort', Params::STRING, false, '排序字段 默认id')]
    #[Params('order', Params::BOOL, false, '倒序')]
    #[Params('page', Params::INT, false, '分页 默认1')]
    #[Params('page_size', Params::INT, false, '分页大小 默认15')]
    public function list(): Json
    {
        $userBiz = UserBiz::list(p());
        return Result::success($userBiz::getData());
    }

    #[Params('name', Params::STRING, true, "名称")]
    #[Params('account', Params::STRING, true, "账号")]
    #[Params('password', Params::STRING, true, "密码")]
    #[Params('memo', Params::STRING, false, "备注")]
    public function add(): Json
    {
        $userBiz = UserBiz::add(p());
        if ($userBiz::hasError()) {
            return Result::fail(...$userBiz::getError());
        }
        return Result::success($userBiz::getData());
    }

    #[Params('id', Params::INT, true, "ID")]
    #[Params('name', Params::STRING, true, "名称")]
    #[Params('memo', Params::STRING, false, "备注")]
    public function edit(): Json
    {
        $userBiz = UserBiz::edit(p());
        if ($userBiz::hasError()) {
            return Result::fail(...$userBiz::getError());
        }
        return Result::success($userBiz::getData());
    }

    #[Params('ids', Params::STRING, true, "ids多个','分隔")]
    public function del(): Json
    {
        $userBiz = UserBiz::del(p());
        if ($userBiz::hasError()) {
            return Result::fail(...$userBiz::getError());
        }
        return Result::success($userBiz::getData());
    }

    #[Params('name', Params::STRING, true, '昵称')]
    #[Params('avatar', Params::STRING, false, '头像')]
    #[Params('old_password', Params::STRING, false, '旧密码')]
    #[Params('new_password', Params::STRING, false, '新密码')]
    #[Params('memo', Params::STRING, false, '备注')]
    public function setting(): Json
    {
        $user = SysUser::findOrEmpty(Cache::get(CacheEnum::UID));
        if ($user->isEmpty()) {
            return Result::fail('user error');
        }
        $user->save([
            'name' => p('name'),
            'avatar' => p('avatar'),
            'memo' => p('memo'),
        ]);

        if (!empty(p('old_password')) && !empty(p('new_password'))) {
            $userBiz = UserBiz::setPassword(p('old_password'), p('new_password'));
            if ($userBiz::hasError()) {
                return Result::fail(...$userBiz::getError());
            }

            UserBiz::logout();
        }

        return Result::success('ok');
    }

    #[Params('user_id', Params::INT, true, '用户ID')]
    #[Params('password', Params::STRING, true, '密码')]
    public function setUserPassword(): Json
    {
        $userBiz = UserBiz::setUserPassword(p('user_id'), p('password'));
        if ($userBiz::hasError()) {
            return Result::fail(...$userBiz::getError());
        }
        return Result::success($userBiz::getData());
    }
}
